home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / DTS Japan-Sample Code / AppleEvent•MacApp3.0b1•7.0 / UCore.cp < prev    next >
Encoding:
Text File  |  1992-02-05  |  47.9 KB  |  1,794 lines  |  [TEXT/MPS ]

  1. // Copyright © 1991 Apple Computer, Inc. All rights reserved.
  2.  
  3. #ifndef    __UCORE__
  4. #include "UCore.h"
  5. #endif
  6.  
  7. extern TCoreApplication * gTaroApplication;
  8.  
  9. // ============================== AppleEvent Handler=================================
  10. pascal OSErr    ReceiveBeginTransaction(AppleEvent & theAppleEvent, AppleEvent & reply,
  11.                                 long /*handlerRefcon*/)
  12. {
  13.     TBeginTransactionCommand * aBeginTransactionCommand = new TBeginTransactionCommand;
  14.     FailNIL(aBeginTransactionCommand);
  15.     aBeginTransactionCommand->IBeginTransactionCommand(theAppleEvent, reply);
  16.     gTaroApplication->PostCommand(aBeginTransactionCommand);
  17.  
  18.     return noErr;
  19. }
  20.  
  21. // ------------------------------------------------------------------------
  22. pascal OSErr    ReceiveEndTransaction(AppleEvent & theAppleEvent, AppleEvent & reply,
  23.                                 long /*handlerRefcon*/)
  24. {
  25.     DescType    typeCode;
  26.     Size        actualSize;
  27.     TAppleEventPlayer * aPlayer;
  28.     FailInfo    fi;
  29.     long    longError = noErr;
  30.  
  31.     if(fi.Try())
  32.     {
  33.         FailOSErr(AEGetAttributePtr(theAppleEvent, keyTransactionIDAttr, typeLongInteger,
  34.                     typeCode, (Ptr)&aPlayer, sizeof(aPlayer), actualSize));
  35.         if(IsObject(aPlayer))
  36.         {
  37.             if(IsMemberClassID(aPlayer, GetClassIDFromName("TAppleEventPlayer")))
  38.             {
  39.                 aPlayer->EndTransaction();
  40.             }
  41.             else
  42.                 longError = errAEEventNotHandled;
  43.         }
  44.         else
  45.             longError = errAEEventNotHandled;
  46.         if(longError != noErr)
  47.             FailOSErr(AEPutParamPtr(reply, keyErrorNumber, typeLongInteger,
  48.                     (Ptr)&longError, sizeof(longError)));
  49.         fi.Success();
  50.     }
  51.     else
  52.     {
  53.         longError = errAEFail;
  54.         FailOSErr(AEPutParamPtr(reply, keyErrorNumber, typeLongInteger,
  55.                     (Ptr)&longError, sizeof(longError)));
  56.         fi.ReSignal();
  57.     }
  58.     return noErr;
  59. }
  60.  
  61. // ------------------------------------------------------------------------
  62. pascal OSErr    ReceiveNewElement(AppleEvent & theAppleEvent, AppleEvent & reply,
  63.                                 long /*handlerRefcon*/)
  64. {
  65.     TNewElementCommand * aNewElementCommand = new TNewElementCommand;
  66.     FailNIL(aNewElementCommand);
  67.     aNewElementCommand->INewElementCommand(theAppleEvent, reply);
  68.     gTaroApplication->InsertNewElementCommand(aNewElementCommand);
  69.  
  70.     return noErr;
  71. }
  72.  
  73. // ------------------------------------------------------------------------
  74. pascal OSErr    ReceiveSetData(AppleEvent & theAppleEvent, AppleEvent & reply,
  75.                                 void * /*refcon*/)
  76. {
  77.     TSetDataCommand * aSetDataCommand = new TSetDataCommand;
  78.     aSetDataCommand->ISetDataCommand(theAppleEvent, reply);
  79.     gTaroApplication->PostCommand(aSetDataCommand);
  80.     return noErr;
  81. }
  82.  
  83. // ------------------------------------------------------------------------
  84. pascal OSErr    ReceiveAbout(AppleEvent & /*theAppleEvent*/, AppleEvent & /*reply*/,
  85.                                 void * /*refcon*/)
  86. {
  87.     TAboutBoxCommand * anAboutBoxCommand = new TAboutBoxCommand;
  88.     anAboutBoxCommand->IAboutBoxCommand(cAboutApp);
  89.     gTaroApplication->PostCommand(anAboutBoxCommand);
  90.     return noErr;
  91. }
  92.  
  93. // ------------------------------------------------------------------------
  94. pascal OSErr    ReceiveUnknown(AppleEvent & /*theAppleEvent*/, AppleEvent & /*reply*/,
  95.                                 long /*handlerRefcon*/)
  96. {
  97.     return noErr;
  98. }
  99.  
  100. // ============================== Service Routines =================================
  101. char    TranslateCellSize(char    anItem)
  102. {
  103.     switch(anItem)
  104.     {
  105.     case 1:
  106.         return 4;
  107.     case 2:
  108.         return 6;
  109.     case 3:
  110.     default:
  111.         return 8;
  112.     case 4:
  113.         return 12;
  114.     case 5:
  115.         return 16;
  116.     case 6:
  117.         return 24;
  118.     case 7:
  119.         return 32;
  120.     }
  121. }
  122.  
  123. // ------------------------------------------------------------------------
  124. char    RetranslateCellSize(char CellSize)
  125. {
  126.     switch(CellSize)
  127.     {
  128.     case 4:
  129.         return 1;
  130.     case 6:
  131.         return 2;
  132.     case 8:
  133.     default:
  134.         return 3;
  135.     case 12:
  136.         return 4;
  137.     case 16:
  138.         return 5;
  139.     case 24:
  140.         return 6;
  141.     case 32:
  142.         return 7;
  143.     }
  144. }
  145.  
  146. // ------------------------------------------------------------------------
  147. #pragma segment Main
  148. void    ShowModalDialog(short DialogID)
  149. {
  150.     short    itemHit;
  151.     
  152.     DialogPtr aDialog = GetNewDialog(DialogID, NULL, (GrafPtr)-1);
  153.     ModalDialog(NULL, itemHit);
  154.     DisposDialog(aDialog);
  155. }
  156.  
  157. // ------------------------------------------------------------------------
  158. #pragma segment Main
  159. OSErr    CheckPPC(void)
  160. {
  161.     const    short    kPPCToolboxDisabled            =    1002;
  162.     const    short    kAppleTalkDisabled            =    1000;
  163.     const    short    kProgramLinkingDisabled        =    1001;
  164.     const    short    kAppleEventNotAvailable        =    1003;
  165.     long    PPCAttributes, AppleEventAttributes;
  166.     OSErr    Error = noErr;
  167.     
  168.     if(Gestalt(gestaltAppleEventsAttr, AppleEventAttributes) != noErr)
  169.     {
  170.         return kAppleEventNotAvailable;
  171.     }
  172.  
  173.     if(Gestalt(gestaltPPCToolboxAttr, PPCAttributes) == noErr)
  174.     {
  175.         if((PPCAttributes & gestaltPPCSupportsOutGoing) == 0)
  176.         {
  177.             Error = kAppleTalkDisabled;
  178.         }
  179.         else if((PPCAttributes & gestaltPPCSupportsIncoming) == 0)
  180.         {
  181.             Error = kProgramLinkingDisabled;
  182.         }
  183.     }
  184.     else
  185.     {
  186.         Error = kPPCToolboxDisabled;
  187.     }
  188.     return Error;
  189. }
  190.  
  191. // ------------------------------------------------------------------------
  192. #pragma segment AClose
  193. void InstallNotification(void)
  194. {
  195.     NMRec    Notification;
  196.     
  197.     Notification.qType = 8;
  198.     Notification.nmMark = 1;
  199.     Notification.nmIcon = GetResource('ics#', 128);
  200.     Notification.nmSound = GetResource('snd ', 1000);
  201.     Notification.nmStr = NULL;
  202.     Notification.nmResp = (NMProcPtr)-1;
  203.     Notification.nmRefCon = NULL;
  204.  
  205.     NMInstall(&Notification);
  206. }
  207.  
  208.  
  209. // ------------------------------------------------------------------------
  210. #pragma segment ARes
  211. pascal OSErr TaroTableAccessor( DescType desiredClass,
  212.         const AEDesc &container, DescType /*containerClass*/, DescType form,
  213.         const AEDesc &selectionData, AEDesc &value, long /*LongInt*/ )
  214. {
  215.     OSErr    anError = errAEEventNotHandled;
  216.     if((desiredClass == cTable) && (container.descriptorType == typeNull))
  217.     {
  218.         if((selectionData.descriptorType == typeChar)  && (form == formName))
  219.         {
  220.             Str255    documentName;
  221.             
  222.             GetIText(selectionData.dataHandle, documentName);
  223.             TDocument * aDocument = gTaroApplication->FindDocumentFromName(documentName);
  224.             if(IsObject(aDocument))
  225.             {
  226.                 FailOSErr(AECreateDesc(typeTObjectPtr, (Ptr)&aDocument, sizeof(aDocument), value));
  227.                 anError = noErr;
  228.             }
  229.         }
  230.     }
  231.     if(anError != noErr)
  232.         FailOSErr(AECreateDesc(typeNull, NULL, 0, value));
  233.     
  234.     return anError;
  235. }
  236.  
  237.  
  238. // ------------------------------------------------------------------------
  239. #pragma segment ARes
  240. pascal OSErr TaroCellAccessor( DescType desiredClass,
  241.         const AEDesc &container, DescType containerClass, DescType form,
  242.         const AEDesc &selectionData, AEDesc &value, long /*LongInt*/ )
  243. {
  244.     OSErr    anError = errAEEventNotHandled;
  245.     if((desiredClass == cCell) && (containerClass == cTable))
  246.     {
  247.         TCoreDocument * aCoreDocument =(TCoreDocument *)(**((TObjectPtrHandle)container.dataHandle));
  248.         if(IsObject(aCoreDocument))
  249.         {
  250.             if(IsMemberClassID(aCoreDocument, GetClassIDFromName("TCoreDocument")))
  251.             {
  252.                 if((selectionData.descriptorType == typeLongInteger)  && (form == formAbsolutePosition))
  253.                 {
  254.                     short    aCellIndex = (short)(**((LongIntHandle)selectionData.dataHandle) - 1);
  255.                     char    aRowSize, aColumnSize;
  256.                     aCoreDocument->GetCellSize(aColumnSize, aRowSize);
  257.                     TRecordCell * aMove = new TRecordCell;
  258.                     aMove->IRecordCell(aCellIndex % aColumnSize, aCellIndex / aColumnSize, kEmpty);
  259.             
  260.                     FailOSErr(AECreateDesc(typeTObjectPtr, (Ptr)&aMove, sizeof(aMove), value));
  261.                     return    noErr;
  262.                 }
  263.             }
  264.         }
  265.     }
  266.     if(anError != noErr)
  267.         FailOSErr(AECreateDesc(typeNull, NULL, 0, value));
  268.     
  269.     return anError;
  270. }
  271.  
  272.  
  273. // ============================== TCoreApplication =================================
  274. #pragma segment AInit
  275. pascal void TCoreApplication::ITaroApplication(void)
  276. {
  277.     IApplication(kFileType, kSignature);
  278.  
  279.     FailOSErr(AEInstallEventHandler(typeWildCard, typeWildCard,
  280.                                             (EventHandlerProcPtr)ReceiveUnknown, 0, FALSE));
  281.  
  282.     FailOSErr(AEInstallEventHandler(kAECoreSuite, kAEBeginTransaction,
  283.                                             (EventHandlerProcPtr)ReceiveBeginTransaction, 0, FALSE));
  284.     FailOSErr(AEInstallEventHandler(kAECoreSuite, kAEEndTransaction,
  285.                                             (EventHandlerProcPtr)ReceiveEndTransaction, 0, FALSE));
  286.     FailOSErr(AEInstallEventHandler(kAECoreSuite, kAENewElement,
  287.                                             (EventHandlerProcPtr)ReceiveNewElement, 0, FALSE));
  288.     FailOSErr(AEInstallEventHandler(kAECoreSuite, kAESetData,
  289.                                             (EventHandlerProcPtr)ReceiveSetData, 0, FALSE));
  290.     FailOSErr(AEInstallEventHandler(kAECoreSuite, kAEAbout,
  291.                                             (EventHandlerProcPtr)ReceiveAbout, 0, FALSE));
  292.  
  293.     FailOSErr(AEInstallObjectAccessor(cTable, typeWildCard,
  294.                                             TaroTableAccessor, 0, FALSE));
  295.     FailOSErr(AEInstallObjectAccessor(cCell, typeWildCard,
  296.                                             TaroCellAccessor, 0, FALSE));
  297.  
  298.     fLaunchWithNewDocument = FALSE;
  299.     
  300.     TList    *    aList = NewList();
  301.     FailNIL(aList);
  302.     fNewElementCommandList = aList;
  303.     fNewElementCommandList->SetEltType("TNewElementCommand");
  304. }
  305.  
  306. // ------------------------------------------------------------------------
  307. #pragma segment AOpen
  308. pascal void TCoreApplication::InsertNewElementCommand(TNewElementCommand * aNewElementCommand)
  309. {
  310.     if(fNewElementCommandList->GetSize() == 0)
  311.         PostCommand(aNewElementCommand);
  312.     fNewElementCommandList->InsertLast(aNewElementCommand);
  313. }
  314.  
  315. // ------------------------------------------------------------------------
  316. #pragma segment AOpen
  317. pascal void TCoreApplication::RemoveNewElementCommand(void)
  318. {
  319.     fNewElementCommandList->AtDelete(1);
  320.     if(fNewElementCommandList->GetSize() != 0)
  321.         PostCommand((TCommand *)fNewElementCommandList->At(1));
  322. }
  323.  
  324. // ------------------------------------------------------------------------
  325. #pragma segment AOpen
  326. pascal TNewElementCommand * TCoreApplication::GetNewElementCommand(void)
  327. {
  328.     if(fNewElementCommandList->GetSize() != 0)
  329.         return    (TNewElementCommand *)fNewElementCommandList->At(1);
  330.     else
  331.         return    NULL;
  332. }
  333.  
  334. // ------------------------------------------------------------------------
  335. #pragma segment AOpen
  336. pascal TDocument * TCoreApplication::FindDocumentFromName(Str255 & theName)
  337. {
  338.     CDocumentIterator    iter(this);
  339.     
  340.     for(TDocument * aDocument = iter.FirstDocument(); iter.More(); aDocument = iter.NextDocument())
  341.     {
  342.         Str255    aName;
  343.     
  344.         aDocument->GetTitle(aName);
  345.         if(aName == theName)
  346.             return aDocument;
  347.     }
  348.     return    NULL;
  349. }
  350.  
  351. // ------------------------------------------------------------------------
  352. #pragma segment AOpen
  353. pascal TAppleEventPlayer * TCoreApplication::CreateServerPlayer(void)
  354. {
  355.     return    NULL;
  356. }
  357.  
  358. // ============================== TCoreDocument =================================
  359. #pragma segment AOpen
  360. pascal void TCoreDocument::ICoreDocument(char aHCellSize, char aVCellSize,
  361.                                         char aBlackPlayer, char aWhitePlayer)
  362. {
  363.     gBusyCursor->ForceBusy();
  364.     this->IDocument();
  365.     fHCellSize = aHCellSize;
  366.     fVCellSize = aVCellSize;
  367.  
  368.     fBlackPlayer = NULL;
  369.     fWhitePlayer = NULL;
  370.  
  371.     fTurn = kBlackTurn;
  372.  
  373.     fIdleFreq =2;
  374.  
  375.     FailNonObject(fBlackPlayer = CreatePlayer(aBlackPlayer, kBlack));
  376.     FailNonObject(fWhitePlayer = CreatePlayer(aWhitePlayer, kWhite));
  377. }
  378.  
  379. // ------------------------------------------------------------------------
  380. #pragma segment AOpen
  381. pascal TPlayer * TCoreDocument::CreatePlayer(char /*PlayerType*/, CellState /*aColor*/)
  382. {
  383.     return    NULL;
  384. }
  385.  
  386. // ------------------------------------------------------------------------
  387. #pragma segment AOpen
  388. pascal void TCoreDocument::CreateTableSpecifier(AEDesc & aTableSpec)
  389. {
  390.     AEDesc    dDocName, dNull;
  391.     Str255    aTitle;
  392.     GetTitle(aTitle);
  393.     FailOSErr(AECreateDesc(typeChar, (char *)aTitle, aTitle.Length(), dDocName));
  394.     FailOSErr(AECreateDesc(typeNull, NULL, 0, dNull));
  395.     FailOSErr(CreateObjSpecifier(cTable, dNull, formName, dDocName, TRUE, aTableSpec));
  396. }
  397.  
  398. // ------------------------------------------------------------------------
  399. #pragma segment AOpen
  400. pascal void TCoreDocument::DoMakeViews(Boolean /*forPrinting*/)
  401. {
  402. }
  403.  
  404. // ------------------------------------------------------------------------
  405. #pragma segment AOpen
  406. pascal void TCoreDocument::SetPlayerStatus(void)
  407. {
  408.     if(IsObject(fBlackPlayer) && IsObject(fWhitePlayer))
  409.     {
  410.         PlayerStatus BlackStatus = fBlackPlayer->GetStatus();
  411.         PlayerStatus WhiteStatus = fWhitePlayer->GetStatus();
  412.  
  413.         if((BlackStatus == kReady) && (WhiteStatus == kReady))
  414.         {
  415.             TSendTurnCommand * aSendTurnCommand = new TSendTurnCommand;
  416.             aSendTurnCommand->ISendTurnCommand(this);
  417.             PostCommand(aSendTurnCommand);
  418.         }
  419.         else if((BlackStatus == kNotReady) || (WhiteStatus == kNotReady))
  420.         {
  421.         }
  422.         else if((BlackStatus == kFailed) || (WhiteStatus == kFailed))
  423.         {
  424.             TCloseDocumentCommand * aCloseCommand = new TCloseDocumentCommand;
  425.             FailNIL(aCloseCommand);
  426.             aCloseCommand->ICloseDocumentCommand(this);
  427.             gTaroApplication->PostCommand(aCloseCommand);
  428.         }
  429.     }
  430. }
  431.  
  432. // ------------------------------------------------------------------------
  433. #pragma segment AClose
  434. pascal void TCoreDocument::Free()
  435. {
  436.     FreeIfObject(fBlackPlayer);
  437.     FreeIfObject(fWhitePlayer);
  438.     inherited::Free();    
  439. }
  440.  
  441. // ------------------------------------------------------------------------
  442. #pragma segment ASelCommand
  443. pascal TPlayer * TCoreDocument::GetPlayer(GameState aState)
  444. {
  445.     if(aState == kWhiteTurn)
  446.         return fWhitePlayer;
  447.     else if(aState == kBlackTurn)
  448.         return fBlackPlayer;
  449.     else
  450.         return NULL;
  451. }
  452.  
  453. // ------------------------------------------------------------------------
  454. #pragma segment ASelCommand
  455. pascal TPlayer * TCoreDocument::GetTemporaryPlayer(void)
  456. {
  457.     return GetPlayer(GetTurn());
  458. }
  459.  
  460. // ------------------------------------------------------------------------
  461. #pragma segment ASelCommand
  462. pascal TPlayer * TCoreDocument::GetAnotherPlayer(void)
  463. {
  464.     GameState aState = GetTurn();
  465.     if(aState == kWhiteTurn)
  466.         return fBlackPlayer;
  467.     else if(aState == kBlackTurn)
  468.         return fWhitePlayer;
  469.     else
  470.         return NULL;
  471. }
  472.  
  473. // ------------------------------------------------------------------------
  474. #pragma segment ASelCommand
  475. pascal GameState TCoreDocument::GetTurn(void)
  476. {
  477.     if(fTurn == kBlackTurnPass)
  478.         return kBlackTurn;
  479.     else if(fTurn == kWhiteTurnPass)
  480.         return kWhiteTurn;
  481.     else
  482.         return fTurn;
  483. }
  484.  
  485. // ------------------------------------------------------------------------
  486. #pragma segment ASelCommand
  487. pascal CellState TCoreDocument::GetTurnCellState(void)
  488. {
  489.     switch (fTurn)
  490.     {
  491.     case    kBlackTurn:
  492.     case    kBlackTurnPass:
  493.         return kBlack;
  494.     case    kWhiteTurn:
  495.     case    kWhiteTurnPass:
  496.         return kWhite;
  497.     default:
  498.         return kEmpty;
  499.     }
  500. }
  501.  
  502. // ------------------------------------------------------------------------
  503. #pragma segment ASelCommand
  504. pascal void TCoreDocument::SwitchTurn(Boolean NotPass)
  505. {
  506.     if(NotPass)
  507.     {
  508.         if((fTurn == kBlackTurn) || (fTurn == kBlackTurnPass))
  509.             fTurn = kWhiteTurn;
  510.         else if((fTurn == kWhiteTurn) || (fTurn == kWhiteTurnPass))
  511.             fTurn = kBlackTurn;
  512.     }
  513.     else
  514.     {
  515.         if((fTurn == kBlackTurnPass) || (fTurn == kWhiteTurnPass))
  516.             GameOver();
  517.         else if(fTurn == kBlackTurn)
  518.             fTurn = kWhiteTurnPass;
  519.         else if(fTurn == kWhiteTurn)
  520.             fTurn = kBlackTurnPass;
  521.     }
  522. }
  523.  
  524. // ------------------------------------------------------------------------
  525. #pragma segment ASelCommand
  526. pascal void TCoreDocument::SendTurn(void)
  527. {
  528.     TPlayer * aPlayer;
  529.     
  530.     if(GetTurn() != kGameOver)
  531.     {
  532.         if(IsObject(aPlayer = GetTemporaryPlayer()))
  533.             aPlayer->MyTurn();
  534.     }
  535. }
  536.  
  537. // ------------------------------------------------------------------------
  538. #pragma segment ASelCommand
  539. pascal void TCoreDocument::PlayerMove(TRecordCell * aPlayerMove)
  540. {
  541.     TPlayer * aPlayer;
  542.     
  543.     if(IsObject(aPlayer = GetAnotherPlayer()))
  544.         aPlayer->YourMove(aPlayerMove);
  545.     if(IsObject(aPlayerMove))
  546.         SwitchTurn(TRUE);            
  547.     else
  548.         SwitchTurn(FALSE);
  549.  
  550.     TSendTurnCommand * aSendTurnCommand = new TSendTurnCommand;
  551.     aSendTurnCommand->ISendTurnCommand(this);
  552.     PostCommand(aSendTurnCommand);
  553. }
  554.  
  555. // ------------------------------------------------------------------------
  556. #pragma segment ASelCommand
  557. pascal void TCoreDocument::GetCellSize(char & aHCellSize, char & aVCellSize)
  558. {
  559.     aHCellSize = fHCellSize;
  560.     aVCellSize = fVCellSize;
  561. }
  562.  
  563. // ------------------------------------------------------------------------
  564. #pragma segment AClose
  565. pascal void TCoreDocument::GameClose(void)
  566. {
  567. }
  568.  
  569. // ------------------------------------------------------------------------
  570. #pragma segment AClose
  571. pascal void TCoreDocument::GameOver(void)
  572. {
  573.     fTurn = kGameOver;
  574. }
  575.  
  576. // ------------------------------------------------------------------------
  577. #pragma segment AClose
  578. pascal void TCoreDocument::Close(void)
  579. {
  580.     GameClose();
  581.     inherited::Close();
  582. }
  583.  
  584.  
  585. // ============================== TScene =================================
  586. #pragma segment AOpen
  587. pascal void TScene::IScene(TCoreDocument * aCoreDocument)
  588. {
  589.     TList *aList;
  590.     char    column, row, aHCellSize, aVCellSize;
  591.     CellPtr    aCell;
  592.     
  593.     fCoreDocument = aCoreDocument;
  594.     fCellList = NULL;                        // in case IDocument fails
  595.     aList = NewList();
  596.     fCellList = aList;
  597.     fCellList->SetEltType("TCell");            // for Inspector window
  598.     
  599.     aList = NewList();
  600.     fNeighborCellList = aList;
  601.     fNeighborCellList->SetEltType("TCell");            // for Inspector window
  602.  
  603.     fCoreDocument->GetCellSize(aHCellSize, aVCellSize);
  604.     for(row = 0; row < aVCellSize; row++)
  605.     {
  606.         for(column = 0; column < aHCellSize; column++)
  607.         {
  608.             FailSpaceIsLow();
  609.             aCell = this->CreateCell(column, row);
  610.         }
  611.     }
  612.  
  613.     fBlack = fWhite = 0;
  614. }
  615.  
  616. // ------------------------------------------------------------------------
  617. #pragma segment AOpen
  618. pascal TCell * TScene::CreateCell(char aColumn, char aRow)
  619. {
  620.     TCell * aCell = new TCell;
  621.     FailNIL(aCell);
  622.     aCell->ICell(this, aColumn, aRow);
  623.     fCellList->InsertLast(aCell);
  624.     return aCell;
  625. }
  626.  
  627. // ------------------------------------------------------------------------
  628. #pragma segment ASelCommand
  629. pascal void TScene::AddCellLast(TCell *aCell)
  630. {
  631.     fCellList->InsertLast(aCell);
  632. }
  633.  
  634. // ------------------------------------------------------------------------
  635. #pragma segment ASelCommand
  636. pascal TCell * TScene::GetCell(char aColumn, char aRow)
  637. {
  638.     char    aHCellSize, aVCellSize;
  639.  
  640.     fCoreDocument->GetCellSize(aHCellSize, aVCellSize);
  641.     return (TCell *)fCellList->At(aColumn + aRow * aHCellSize + 1);
  642. }
  643.  
  644. // ------------------------------------------------------------------------
  645. #pragma segment ASelCommand
  646. pascal CellState TScene::GetTurn(void)
  647. {
  648.     return fTurn;
  649. }
  650.  
  651. // ------------------------------------------------------------------------
  652. #pragma segment ASelCommand
  653. pascal void TScene::SetTurn(CellState aCellState)
  654. {
  655.     fTurn = aCellState;
  656. }
  657.  
  658. // ------------------------------------------------------------------------
  659. #pragma segment AClose
  660. pascal void TScene::GameOver(void)
  661. {
  662.     fCoreDocument->GameOver();
  663. }
  664.  
  665. // ------------------------------------------------------------------------
  666. #pragma segment AClose
  667. pascal void TScene::Free()
  668. {
  669.     if (IsObject(fCellList))
  670.         fCellList->FreeList();    // frees Cell objects and List object
  671.     FreeIfObject(fNeighborCellList);
  672.     inherited::Free();
  673. }
  674.  
  675.  
  676. // ------------------------------------------------------------------------
  677. #pragma segment ASelCommand
  678. pascal void TScene::AddNeighborCellLast(TCell *aCell)
  679. {
  680.     fNeighborCellList->InsertLast(aCell);
  681. }
  682.  
  683. // ------------------------------------------------------------------------
  684. #pragma segment ARes
  685. pascal TList * TScene::GetNeighborCellList(void)
  686. {
  687.     return fNeighborCellList;
  688. }
  689.  
  690. // ------------------------------------------------------------------------
  691. #pragma segment ARes
  692. pascal void TScene::ChangeCount(CellState aColor, short aNumber)
  693. {
  694.     if(aColor == kWhite)
  695.         fWhite += aNumber;
  696.     else if(aColor == kBlack)
  697.         fBlack += aNumber;
  698. }
  699.  
  700. // ------------------------------------------------------------------------
  701. #pragma segment ARes
  702. pascal short TScene::GetCount(CellState aColor)
  703. {
  704.     if(aColor == kWhite)
  705.         return fWhite;
  706.     else if(aColor == kBlack)
  707.         return fBlack;
  708. }
  709.  
  710. // ------------------------------------------------------------------------
  711. #pragma segment ARes
  712. pascal TCoreDocument * TScene::ReturnCoreDocument(void)
  713. {
  714.     return fCoreDocument;
  715. }
  716.  
  717. // ------------------------------------------------------------------------
  718. #pragma segment ARes
  719. pascal void TScene::SearchAvailableCell(void)
  720. {
  721.     CObjectIterator iter(fNeighborCellList);
  722.         
  723.     fAvailable = 0;
  724.     for (TCell * aCell = (TCell *)iter.FirstObject(); iter.More(); aCell = (TCell *)iter.NextObject())
  725.         if(aCell->IsNeighbor())
  726.             aCell->ScanAllDriection();
  727. }
  728.  
  729. // ------------------------------------------------------------------------
  730. #pragma segment ARes
  731. pascal TCell * TScene::SearchNeighborCellList(TRecordCell *theCoordinate)
  732. {
  733.     CObjectIterator iter(fNeighborCellList);
  734.         
  735.     for (TCell * aCell = (TCell *)iter.FirstObject(); iter.More(); aCell = (TCell *)iter.NextObject())
  736.         if(aCell->IsEqual(theCoordinate))
  737.             return aCell;
  738. }
  739.  
  740. // ------------------------------------------------------------------------
  741. #pragma segment ARes
  742. pascal void TScene::DeleteNeighborCell(TCell * aCell)
  743. {
  744.     fNeighborCellList->Delete(aCell);
  745. }
  746.  
  747. // ------------------------------------------------------------------------
  748. #pragma segment ARes
  749. pascal short TScene::HasAvailableCell(void)
  750. {
  751.     SearchAvailableCell();
  752.     return fAvailable;
  753. }
  754.  
  755. // ------------------------------------------------------------------------
  756. #pragma segment ARes
  757. pascal short TScene::GetAvailableCell(void)
  758. {
  759.     return fAvailable;
  760. }
  761.  
  762. // ------------------------------------------------------------------------
  763. #pragma segment ARes
  764. pascal void TScene::AddAvailable(void)
  765. {
  766.     fAvailable++;
  767. }
  768.  
  769. // ------------------------------------------------------------------------
  770. #pragma segment ARes
  771. pascal void TScene::ResetAvailable(void)
  772. {
  773.     fAvailable = 0;
  774. }
  775.  
  776. // ------------------------------------------------------------------------
  777. #pragma segment AOpen
  778. pascal void TScene::SetInitialState(void)
  779. {
  780.     char    aHCellSize, aVCellSize;
  781.  
  782.     ReturnCoreDocument()->GetCellSize(aHCellSize, aVCellSize);
  783.     SetTurn(kWhite);
  784.     GetCell(aHCellSize / 2 - 1, aVCellSize / 2) ->PutPiece();
  785.     GetCell(aHCellSize / 2, aVCellSize / 2 - 1) ->PutPiece();
  786.     SetTurn(kBlack);
  787.     GetCell(aHCellSize / 2, aVCellSize / 2) ->PutPiece();
  788.     GetCell(aHCellSize / 2 - 1, aVCellSize / 2 - 1) ->PutPiece();
  789. }
  790.  
  791.  
  792. // ============================== TCoordinate =================================
  793. #pragma segment ASelCommand
  794. pascal void TCoordinate::ICoordinate(const char aColumn, const char aRow)
  795. {
  796.     fColumn = aColumn;
  797.     fRow = aRow;
  798. }
  799.  
  800. // ------------------------------------------------------------------------
  801. #pragma segment ARes
  802. pascal char TCoordinate::GetRow(void)    
  803. {
  804.     return fRow;
  805. }
  806.  
  807. // ------------------------------------------------------------------------
  808. #pragma segment ARes
  809. pascal char TCoordinate::GetColumn(void)    
  810. {
  811.     return fColumn;
  812. }
  813.  
  814. #pragma segment ARes
  815. // ------------------------------------------------------------------------
  816. pascal Boolean TCoordinate::IsEqual(TCoordinate *theCoordinate)
  817. {
  818.     if((fRow == theCoordinate->GetRow()) &&
  819.         (fColumn == theCoordinate->GetColumn()))
  820.         return TRUE;
  821.     else
  822.         return FALSE;
  823. }
  824.  
  825.  
  826.  
  827. // ============================== TRecordCell =================================
  828. #pragma segment ASelCommand
  829. pascal void TRecordCell::IRecordCell(const char aColumn, const char aRow, CellState aState)
  830. {
  831.     ICoordinate(aColumn, aRow);
  832.     fState = aState;
  833. }
  834.  
  835. // ------------------------------------------------------------------------
  836. #pragma segment ARes
  837. pascal CellState TRecordCell::GetState(void)    
  838. {
  839.     return fState;
  840. }
  841.  
  842. // ------------------------------------------------------------------------
  843. #pragma segment ARes
  844. pascal GameState TRecordCell::GetGameState(void)    
  845. {
  846.     if(fState == kBlack)
  847.         return kBlackTurn;
  848.     else if(fState == kWhite)
  849.         return kWhiteTurn;
  850.     else
  851.         return kGameOver;
  852. }
  853.  
  854. // ------------------------------------------------------------------------
  855. #pragma segment ARes
  856. pascal void TRecordCell::SetState(CellState aState)    
  857. {
  858.     fState = aState;
  859. }
  860.  
  861. // ------------------------------------------------------------------------
  862. #pragma segment ARes
  863. pascal Boolean TRecordCell::IsEmpty(void)    
  864. {
  865.     if((fState == kEmpty) || (fState == kNeighbor) || (fState == kAvailable))
  866.         return TRUE;
  867.     else
  868.         return FALSE;
  869. }
  870.  
  871. // ------------------------------------------------------------------------
  872. #pragma segment ARes
  873. pascal Boolean TRecordCell::IsNeighbor(void)    
  874. {
  875.     if((fState == kNeighbor) || (fState == kAvailable))
  876.         return TRUE;
  877.     else
  878.         return FALSE;
  879. }
  880.  
  881. // ------------------------------------------------------------------------
  882. #pragma segment ARes
  883. pascal Boolean TRecordCell::IsAvailable(void)    
  884. {
  885.     if(fState == kAvailable)
  886.         return TRUE;
  887.     else
  888.         return FALSE;
  889. }
  890.  
  891. // ------------------------------------------------------------------------
  892. #pragma segment ARes
  893. pascal void TRecordCell::SetNeighbor(void)    
  894. {
  895.     if((fState == kEmpty) || (fState == kAvailable))
  896.         fState = kNeighbor;
  897. }
  898.  
  899. // ------------------------------------------------------------------------
  900. #pragma segment ARes
  901. pascal void TRecordCell::SetAvailable(void)    
  902. {
  903.     if((fState == kEmpty) || (fState == kNeighbor))
  904.         fState = kAvailable;
  905. }
  906.  
  907. // ------------------------------------------------------------------------
  908. #pragma segment ARes
  909. pascal TRecordCell * TRecordCell::CloneRecordCell(void)    
  910. {
  911.     TRecordCell * aRecordCell = new TRecordCell;
  912.     FailNIL(aRecordCell);
  913.     aRecordCell->IRecordCell(GetColumn(), GetRow(), fState);
  914.     return aRecordCell;
  915. }
  916.  
  917.  
  918.  
  919. // ============================== TCell =================================
  920. #pragma segment ASelCommand
  921. pascal void TCell::ICell(TScene * aScene, const char aColumn, const char aRow)
  922. {
  923.     IRecordCell(aColumn, aRow, kEmpty);
  924.     fScene = aScene;
  925. }
  926.  
  927. // ------------------------------------------------------------------------
  928. #pragma segment ARes
  929. pascal TScene * TCell::GetScene(void)    
  930. {
  931.     return fScene;
  932. }
  933.  
  934. // ------------------------------------------------------------------------
  935. #pragma segment ARes
  936. pascal void TCell::PutPiece(void)    
  937. {
  938.     Direction    aDirection;
  939.     
  940.     SetState(kAvailable);
  941.     for(aDirection = kNorth; aDirection <= kNorthWest; aDirection++)
  942.         ScanOneDriection(aDirection, kSame, this);
  943.     ChangeState();
  944.     GetScene()->DeleteNeighborCell(this);
  945. }
  946.  
  947. // ------------------------------------------------------------------------
  948. #pragma segment ADoCommand
  949. pascal void TCell::SetNeighbor(void)
  950. {
  951.     if(IsEmpty() && !IsNeighbor())
  952.         GetScene()->AddNeighborCellLast(this);
  953.     inherited::SetNeighbor();
  954. }
  955.  
  956. // ------------------------------------------------------------------------
  957. #pragma segment ARes
  958.  
  959. TestState    TestCellState(CellState State1, CellState State2)
  960. {
  961.     if(((State1 == kWhite) && (State2 == kBlack)) ||
  962.         ((State1 == kBlack) && (State2 == kWhite)))
  963.         return kAnother;
  964.     else if(((State1 == kWhite) && (State2 == kWhite)) ||
  965.         ((State1 == kBlack) && (State2 == kBlack)))
  966.         return kSame;
  967.     else
  968.         return kNoHit;
  969. }
  970.  
  971. // ------------------------------------------------------------------------
  972. #pragma segment ARes
  973. pascal void TCell::ChangeState(void)    
  974. {
  975.     CellState    aTurn;
  976.     
  977.     aTurn = fScene->GetTurn();
  978.     if(IsEmpty())
  979.     {
  980.         SetNeighborCell();
  981.         fScene->ChangeCount(aTurn, 1);
  982.     }
  983.     else if(TestCellState(GetState(), aTurn) == kAnother)
  984.     {
  985.         fScene->ChangeCount(aTurn, 1);
  986.         fScene->ChangeCount(GetState(), -1);
  987.     }
  988.     SetState(aTurn);
  989. }
  990.  
  991. // ------------------------------------------------------------------------
  992. #pragma segment ARes
  993. pascal void TCell::GetCornerCoordinate(char & h, char & v,
  994.                                             char & aHCellSize, char & aVCellSize)    
  995. {
  996.     char    aColumn, aRow;
  997.     
  998.     (fScene->ReturnCoreDocument())->GetCellSize(aHCellSize, aVCellSize);
  999.     aColumn = GetColumn();
  1000.     aRow = GetRow();
  1001.     h = (char)Min(aColumn, aHCellSize - aColumn - 1);
  1002.     v = (char)Min(aRow, aVCellSize - aRow - 1);
  1003. }
  1004.  
  1005. // ------------------------------------------------------------------------
  1006. #pragma segment ADoCommand
  1007. pascal TCell * TCell::GetNeighborCell(Direction aDirection)    
  1008. {
  1009.     char    aColumn, aRow, aHCellSize, aVCellSize;
  1010.     
  1011.     (fScene->ReturnCoreDocument())->GetCellSize(aHCellSize, aVCellSize);
  1012.     aColumn = GetColumn();
  1013.     switch(aDirection)
  1014.     {
  1015.     case    kNorthEast:
  1016.     case    kEast:
  1017.     case    kSouthEast:
  1018.         if(++aColumn > aHCellSize - 1)
  1019.             return NULL;
  1020.         break;
  1021.     case    kNorth:
  1022.     case    kSouth:
  1023.         break;
  1024.     case    kSouthWest:
  1025.     case    kWest:
  1026.     case    kNorthWest:
  1027.         if(--aColumn < 0)
  1028.             return NULL;
  1029.         break;
  1030.     }
  1031.     aRow = GetRow();
  1032.     switch(aDirection)
  1033.     {
  1034.     case    kSouthEast:
  1035.     case    kSouth:
  1036.     case    kSouthWest:
  1037.         if(++aRow > aVCellSize - 1)
  1038.             return NULL;
  1039.         break;
  1040.     case    kEast:
  1041.     case    kWest:
  1042.         break;
  1043.     case    kNorthWest:
  1044.     case    kNorth:
  1045.     case    kNorthEast:
  1046.         if(--aRow < 0)
  1047.             return NULL;
  1048.         break;
  1049.     }
  1050.     return fScene->GetCell(aColumn, aRow);
  1051. }
  1052.  
  1053. // ------------------------------------------------------------------------
  1054. #pragma segment ADoCommand
  1055. pascal void TCell::ScanAllDriection(void)
  1056. {
  1057.     Direction    aDirection;
  1058.     Boolean        anAvailable;
  1059.     
  1060.     anAvailable = FALSE;
  1061.     SetNeighbor();
  1062.     if(IsEmpty())
  1063.         for(aDirection = kNorth; aDirection <= kNorthWest; aDirection++)
  1064.             if(ScanOneDriection(aDirection, kSame, this) == kHit)
  1065.                 anAvailable = TRUE;
  1066.     if(anAvailable)
  1067.     {
  1068.         SetAvailable();
  1069.         GetScene()->AddAvailable();
  1070.     }
  1071. }
  1072.  
  1073. // ------------------------------------------------------------------------
  1074. #pragma segment ADoCommand
  1075. pascal void TCell::SetNeighborCell(void)
  1076. {
  1077.     Direction    aDirection;
  1078.     TCell * aCell;
  1079.     
  1080.     for(aDirection = kNorth; aDirection <= kNorthWest; aDirection++)
  1081.         if((aCell = GetNeighborCell(aDirection)) != NULL)
  1082.             aCell->SetNeighbor();
  1083. }
  1084.  
  1085. // ------------------------------------------------------------------------
  1086. #pragma segment ADoCommand
  1087. pascal void TCell::HitCell(TCell * aTurnCell)
  1088. {
  1089.     if(IsAvailable())
  1090.         aTurnCell->ChangeState();
  1091. }
  1092.  
  1093. // ------------------------------------------------------------------------
  1094. #pragma segment ADoCommand
  1095. pascal TestState TCell::ScanOneDriection(    Direction aDirection,
  1096.                                             TestState aTestState,
  1097.                                             TCell * anOriginalCell)    
  1098. {
  1099.     TCell * NextCell;
  1100.     CellState    NextState, aTurn;
  1101.     TestState    Condition;
  1102.     
  1103.     aTurn = GetScene()->GetTurn();
  1104.     if((NextCell = GetNeighborCell(aDirection)) == NULL)
  1105.         return kNoHit;
  1106.     if(NextCell->IsEmpty())
  1107.         return kNoHit;
  1108.     NextState = NextCell->GetState();
  1109.     Condition = TestCellState(aTurn, NextState);
  1110.     if(Condition == kSame)
  1111.     {
  1112.         if(aTestState == kAnother)
  1113.             return kHit;
  1114.         else
  1115.             return kNoHit;
  1116.     }
  1117.     if(Condition == kAnother)
  1118.     {
  1119.         if(NextCell->ScanOneDriection(aDirection, kAnother, anOriginalCell) == kHit)
  1120.         {
  1121.             anOriginalCell->HitCell(NextCell);
  1122.             return kHit;
  1123.         }
  1124.         else
  1125.             return kNoHit;
  1126.     }
  1127.     else
  1128.         return kNoHit;
  1129. }
  1130.     
  1131.  
  1132. // ============================== TPlayer =================================
  1133. #pragma segment AOpen
  1134. pascal void TPlayer::Initialize(void)    
  1135. {
  1136.     inherited::Initialize();
  1137.     fCoreDocument = NULL;
  1138.     fPermission = FALSE;
  1139.     fStatus = kNotReady;
  1140. }
  1141.  
  1142. // ------------------------------------------------------------------------
  1143. #pragma segment AOpen
  1144. pascal void TPlayer::IPlayer(TCoreDocument * aCoreDocument)    
  1145. {
  1146.     IObject();
  1147.     fCoreDocument = aCoreDocument;
  1148. }
  1149.  
  1150. // ------------------------------------------------------------------------
  1151. #pragma segment ASelCommand
  1152. pascal void TPlayer::MyTurn(void)    
  1153. {
  1154.     fPermission = TRUE;
  1155. }
  1156.  
  1157. // ------------------------------------------------------------------------
  1158. #pragma segment ASelCommand
  1159. pascal void TPlayer::MyMove(TRecordCell * aRecordCell)    
  1160. {
  1161.     if(fPermission)
  1162.     {
  1163.         fPermission = FALSE;
  1164.         fCoreDocument->PlayerMove(aRecordCell);
  1165.     }
  1166. }
  1167.  
  1168. // ------------------------------------------------------------------------
  1169. #pragma segment ASelCommand
  1170. pascal void TPlayer::YourMove(TRecordCell * /*aRecordCell*/)    
  1171. {
  1172. }
  1173.  
  1174. // ------------------------------------------------------------------------
  1175. #pragma segment ASelCommand
  1176. pascal TCoreDocument * TPlayer::GetCoreDocument(void)    
  1177. {
  1178.     return fCoreDocument;
  1179. }
  1180.  
  1181. // ------------------------------------------------------------------------
  1182. #pragma segment AOpen
  1183. pascal void TPlayer::SetCoreDocument(TCoreDocument * aCoreDocument)    
  1184. {
  1185.     fCoreDocument = aCoreDocument;
  1186. }
  1187.  
  1188. // ------------------------------------------------------------------------
  1189. #pragma segment ASelCommand
  1190. pascal Boolean TPlayer::GetPermission(void)    
  1191. {
  1192.     return fPermission;
  1193. }
  1194.  
  1195. // ------------------------------------------------------------------------
  1196. #pragma segment ASelCommand
  1197. pascal PlayerStatus TPlayer::GetStatus(void)    
  1198. {
  1199.     return fStatus;
  1200. }
  1201.  
  1202. // ------------------------------------------------------------------------
  1203. #pragma segment ASelCommand
  1204. pascal void TPlayer::SetStatus(PlayerStatus aStatus)    
  1205. {
  1206.     fStatus = aStatus;
  1207.     TStartDocumentCommand * aStartCommand = new TStartDocumentCommand;
  1208.     FailNIL(aStartCommand);
  1209.     aStartCommand->IStartDocumentCommand(fCoreDocument);
  1210.     gTaroApplication->PostCommand(aStartCommand);
  1211. }
  1212.  
  1213. // ------------------------------------------------------------------------
  1214. #pragma segment ASelCommand
  1215. pascal void TPlayer::MouseMove(TRecordCell * /*aMouseMove*/)
  1216. {
  1217. }
  1218.  
  1219. // ------------------------------------------------------------------------
  1220. #pragma segment AOpen
  1221. pascal void TPlayer::GetName(Str255 &/*aString*/)
  1222. {
  1223. }
  1224.  
  1225. // ------------------------------------------------------------------------
  1226. #pragma segment AClose
  1227. pascal void TPlayer::GameOver(GameResult /*aResult*/)    
  1228. {
  1229. }
  1230.  
  1231.  
  1232. // ============================== TAppleEventPlayer =================================
  1233. #pragma segment AOpen
  1234. pascal void TAppleEventPlayer::Initialize(void)    
  1235. {
  1236.     inherited::Initialize();
  1237.     fTargetTransactionID = 0;
  1238.     fTarget = NULL;
  1239.     fMove = NULL;
  1240.     fWait = FALSE;
  1241.     AEDesc    dNULL;
  1242.     FailOSErr(AECreateDesc(typeNull, NULL, 0, dNULL));
  1243.     fObjSpec = dNULL;
  1244. }
  1245.  
  1246. // ------------------------------------------------------------------------
  1247. #pragma segment AOpen
  1248. pascal void TAppleEventPlayer::IAppleEventPlayer(TCoreDocument * aCoreDocument, CellState aColor)    
  1249. {
  1250.     IPlayer(aCoreDocument);
  1251.     fMyColor = aColor;
  1252. }
  1253.  
  1254. // ------------------------------------------------------------------------
  1255. #pragma segment AOpen
  1256. pascal unsigned long TAppleEventPlayer::GetTargetTransactionID(void)
  1257. {
  1258.     return fTargetTransactionID;
  1259. }
  1260.  
  1261. // ------------------------------------------------------------------------
  1262. #pragma segment AOpen
  1263. pascal void TAppleEventPlayer::SetTargetTransactionID(unsigned long aTargetTransactionID)
  1264. {
  1265.     fTargetTransactionID = aTargetTransactionID;
  1266. }
  1267.  
  1268. // ------------------------------------------------------------------------
  1269. #pragma segment AOpen
  1270. pascal CellState TAppleEventPlayer::GetMyColor(void)
  1271. {
  1272.     return fMyColor;
  1273. }
  1274.  
  1275. // ------------------------------------------------------------------------
  1276. #pragma segment AOpen
  1277. pascal void TAppleEventPlayer::SetMyColor(CellState aColor)
  1278. {
  1279.     fMyColor = aColor;
  1280. }
  1281.  
  1282. // ------------------------------------------------------------------------
  1283. #pragma segment AClose
  1284. pascal void TAppleEventPlayer::GameOver(GameResult aResult)    
  1285. {
  1286.     inherited::GameOver(aResult);
  1287.     if(fTargetTransactionID != 0)
  1288.     {
  1289.         fTarget->SendEndTransaction(aResult, fTargetTransactionID, this);
  1290.         fTargetTransactionID = 0;
  1291.     }
  1292. }
  1293.  
  1294. // ------------------------------------------------------------------------
  1295. #pragma segment AClose
  1296. pascal void TAppleEventPlayer::EndTransaction(void)    
  1297. {
  1298.     fTargetTransactionID = 0;
  1299.     TCloseDocumentCommand * aCloseCommand = new TCloseDocumentCommand;
  1300.     FailNIL(aCloseCommand);
  1301.     aCloseCommand->ICloseDocumentCommand(GetCoreDocument());
  1302.     gTaroApplication->PostCommand(aCloseCommand);
  1303. }
  1304.  
  1305. // ------------------------------------------------------------------------
  1306. #pragma segment ASelCommand
  1307. pascal void TAppleEventPlayer::MyMove(TRecordCell * aMove)    
  1308. {
  1309.     if(GetPermission())
  1310.         inherited::MyMove(aMove);
  1311.     else
  1312.     {
  1313.         fMove = aMove;
  1314.         fWait = TRUE;
  1315.     }
  1316. }
  1317.  
  1318. // ------------------------------------------------------------------------
  1319. #pragma segment ASelCommand
  1320. pascal void TAppleEventPlayer::MyTurn(void)    
  1321. {
  1322.     inherited::MyTurn();
  1323.     if(fWait)
  1324.     {
  1325.         TMoveCommand * aMoveCommand = new TMoveCommand;
  1326.         FailNIL(aMoveCommand);
  1327.         aMoveCommand->IMoveCommand(this, fMove);
  1328.         gTaroApplication->PostCommand(aMoveCommand);
  1329.  
  1330.         fMove = NULL;
  1331.         fWait = FALSE;
  1332.     }
  1333. }
  1334.  
  1335. // ------------------------------------------------------------------------
  1336. #pragma segment ASelCommand
  1337. pascal void TAppleEventPlayer::YourMove(TRecordCell * aMove)    
  1338. {
  1339.     AEDesc    dCellSpec;
  1340.     
  1341.     CreateTargetCellSpecifier(aMove, dCellSpec);
  1342.     fTarget->SendSetData(aMove, fTargetTransactionID, dCellSpec);
  1343.     FailOSErr(AEDisposeDesc(dCellSpec));
  1344. }
  1345.  
  1346. // ------------------------------------------------------------------------
  1347. #pragma segment ASelCommand
  1348. pascal TTarget * TAppleEventPlayer::ReturnTarget(void)    
  1349. {
  1350.     return fTarget;
  1351. }
  1352.  
  1353. // ------------------------------------------------------------------------
  1354. #pragma segment ASelCommand
  1355. pascal void TAppleEventPlayer::SetTarget(TTarget * aTarget)    
  1356. {
  1357.     fTarget = aTarget;
  1358. }
  1359.  
  1360.  
  1361. // ------------------------------------------------------------------------
  1362. #pragma segment ASelCommand
  1363. pascal void TAppleEventPlayer::SetTargetTableSpecifier(AEDesc & aTargetSpec)    
  1364. {
  1365.     AEDesc    anAEDesc;
  1366.     FailOSErr(AEDuplicateDesc(aTargetSpec, anAEDesc));
  1367.     fObjSpec = anAEDesc;
  1368. }
  1369.  
  1370.  
  1371. // ------------------------------------------------------------------------
  1372. #pragma segment ASelCommand
  1373. pascal void TAppleEventPlayer::Free(void)    
  1374. {
  1375.     AEDesc    anAEDesc = fObjSpec;
  1376.     FailOSErr(AEDisposeDesc(anAEDesc));
  1377.     if(fWait)
  1378.         FreeIfObject(fMove);
  1379.     inherited::Free();
  1380. }
  1381.  
  1382. // ------------------------------------------------------------------------
  1383. #pragma segment ARes
  1384. pascal void TAppleEventPlayer::CreateTargetCellSpecifier(TRecordCell * aCell, AEDesc & dCellSpec)
  1385. {
  1386.     AEDesc    dOffset;
  1387.     AEDesc    dTableSpec;
  1388.     if(IsObject(aCell))
  1389.     {
  1390.         char    aHSize, aVSize;
  1391.     
  1392.         GetCoreDocument()->GetCellSize(aHSize, aVSize);
  1393.         FailOSErr(CreateOffsetDescriptor(aCell->GetRow() * aHSize + aCell->GetColumn() + 1, dOffset));
  1394.     }
  1395.     else
  1396.         FailOSErr(CreateOffsetDescriptor(0, dOffset));
  1397.     AEDesc    anAEDesc = fObjSpec;
  1398.     FailOSErr(AEDuplicateDesc(anAEDesc, dTableSpec));
  1399.     FailOSErr(CreateObjSpecifier(cCell, dTableSpec, formAbsolutePosition, dOffset, TRUE, dCellSpec));
  1400. }
  1401.  
  1402. // ============================== TTarget =================================
  1403. #pragma segment AOpen
  1404. pascal void TTarget::ITarget(AEAddressDesc & address)    
  1405. {
  1406.     IObject();
  1407.     fTargetAddress = address;
  1408. }
  1409.  
  1410. // ------------------------------------------------------------------------
  1411. #pragma segment AOpen
  1412. pascal void TTarget::CreateDesc(DescType typeCode, Ptr dataPtr, Size dataSize)    
  1413. {
  1414.     AEAddressDesc    address;
  1415.     
  1416.     FailOSErr(AECreateDesc(typeCode, dataPtr, dataSize, address));
  1417.     ITarget(address);
  1418. }
  1419.  
  1420. // ------------------------------------------------------------------------
  1421. #pragma segment ASelCommand
  1422. pascal void TTarget::Free(void)    
  1423. {
  1424.     AEDesc    anAEDesc = fTargetAddress;
  1425.     FailOSErr(AEDisposeDesc(anAEDesc));
  1426.     inherited::Free();
  1427. }
  1428.  
  1429. // ------------------------------------------------------------------------
  1430. #pragma segment ASelCommand
  1431. pascal void TTarget::SendSetData(TRecordCell * aMove, unsigned long    aTransactionID, AEDesc & cellSpec)    
  1432. {
  1433.     TAppleEvent    * theAppleEvent;
  1434.     long    aData;
  1435.  
  1436.     theAppleEvent = CreateAppleEvent(kAECoreSuite, kAESetData, kSetDataReturnID, aTransactionID);
  1437.     theAppleEvent->WriteParameter(keyDirectObject, cellSpec);
  1438.     if(IsObject(aMove))
  1439.         aData = aMove->GetState();
  1440.     else
  1441.         aData = -1;
  1442.     theAppleEvent->WriteParameterPtr(keyAETheData, typeEnumerated, (Ptr)&aData, sizeof(aData));
  1443.     theAppleEvent->SetSendingMode(kAENoReply + kAECanInteract + kAECanSwitchLayer);
  1444.     theAppleEvent->Send();
  1445.     theAppleEvent->Free();
  1446. }
  1447.  
  1448. // ------------------------------------------------------------------------
  1449. #pragma segment AClose
  1450. pascal void TTarget::SendEndTransaction(GameResult aResult, unsigned long theServer, TPlayer * /*theClient*/)    
  1451. {
  1452.     if(aResult == kClose)
  1453.     {
  1454.         TAppleEvent    * theAppleEvent = CreateAppleEvent(kAECoreSuite, kAEEndTransaction, kEndTransactionReturnID, theServer);
  1455.         theAppleEvent->SetSendingMode(kAENoReply);
  1456.         theAppleEvent->Send();
  1457.         theAppleEvent->Free();
  1458.     }
  1459. }
  1460.  
  1461. // ------------------------------------------------------------------------
  1462. #pragma segment ASelCommand
  1463. pascal TAppleEvent * TTarget::CreateAppleEvent(AEEventClass theAEEventClass,
  1464.                                 AEEventID theAEEventID,
  1465.                                 short returnID,
  1466.                                 long transactionID)    
  1467. {
  1468.     AppleEvent    aMessage;
  1469.     AEAddressDesc    anAEAddressDesc = fTargetAddress;
  1470.     FailOSErr(AECreateAppleEvent(theAEEventClass, theAEEventID, anAEAddressDesc, returnID, transactionID, aMessage));
  1471.     TAppleEvent * anAppleEvent = new TAppleEvent;
  1472.     anAppleEvent->InitializeFromMessage(aMessage, TRUE);
  1473.     return    anAppleEvent;
  1474. }
  1475.  
  1476. // ------------------------------------------------------------------------
  1477. #pragma segment ASelCommand
  1478. pascal void TTarget::GetName(Str255 & aName)    
  1479. {
  1480.     aName = "";
  1481. }
  1482.  
  1483.  
  1484. // ============================== TServerTarget =================================
  1485. #pragma segment AOpen
  1486. pascal void TServerTarget::IServerTarget(AEAddressDesc & targetID)    
  1487. {
  1488.     ITarget(targetID);
  1489. }
  1490.  
  1491. // ------------------------------------------------------------------------
  1492. #pragma segment ASelCommand
  1493. pascal void TServerTarget::SetName(const Str255 & userName)    
  1494. {
  1495.     fUserName = userName;
  1496. }
  1497.  
  1498. // ------------------------------------------------------------------------
  1499. #pragma segment ASelCommand
  1500. pascal void TServerTarget::GetName(Str255 & aName)    
  1501. {
  1502.     aName = fUserName;
  1503. }
  1504.  
  1505.  
  1506. // ============================== TNoChangesCommand =================================
  1507. #pragma segment AOpen
  1508. pascal void TNoChangesCommand::INoChangesCommand(void)
  1509. {
  1510.     ICommand(0, NULL, FALSE, FALSE, NULL);
  1511. }
  1512.  
  1513.  
  1514. // ============================== TCloseDocumentCommand =================================
  1515. #pragma segment AOpen
  1516. pascal void TCloseDocumentCommand::ICloseDocumentCommand(TCoreDocument * aDocument)
  1517. {
  1518.     fDocument = aDocument;
  1519.     INoChangesCommand();
  1520. }
  1521.  
  1522. // ------------------------------------------------------------------------
  1523. #pragma segment ASelCommand
  1524. pascal void TCloseDocumentCommand::DoIt(void)    
  1525. {
  1526.     fDocument->Close();
  1527.     fCommandDone = TRUE;
  1528. }
  1529.  
  1530.  
  1531. // ============================== TStartDocumentCommand =================================
  1532. #pragma segment AOpen
  1533. pascal void TStartDocumentCommand::IStartDocumentCommand(TCoreDocument * aDocument)
  1534. {
  1535.     fDocument = aDocument;
  1536.     INoChangesCommand();
  1537. }
  1538.  
  1539. // ------------------------------------------------------------------------
  1540. #pragma segment ASelCommand
  1541. pascal void TStartDocumentCommand::DoIt(void)    
  1542. {
  1543.     if(IsObject(fDocument))
  1544.         fDocument->SetPlayerStatus();
  1545.     fCommandDone = TRUE;
  1546. }
  1547.  
  1548.  
  1549. // ============================== TNewElementCommand =================================
  1550. #pragma segment AOpen
  1551. pascal void TNewElementCommand::INewElementCommand(AppleEvent & theAppleEvent, AppleEvent & Reply)
  1552. {
  1553.     DescType    typeCode;
  1554.     Size        actualSize;
  1555.     OSType    classType;
  1556.     
  1557.     InitializeFromAppleEvent(cNewElement, NULL, FALSE, FALSE, NULL, theAppleEvent, Reply);
  1558.     fMessage->ReadParameterPtr(keyAEObjectClass, typeType, typeCode, (Ptr)&classType, sizeof(classType), actualSize);
  1559.  
  1560.     if(classType == cTable)
  1561.     {
  1562.         short        aHCellSize, aVCellSize;
  1563.         long    aColor;
  1564.         AEDesc        ObjSpec, targetSpec;
  1565.         AERecord    ElementList;
  1566.         
  1567.         Str255    aName;
  1568.                 
  1569.         fMessage->ReadParameter(keyAEInsertHere, typeWildCard, ObjSpec);
  1570.         fMessage->ReadParameter(keyAEData, typeAERecord, ElementList);
  1571.         FailOSErr(AEGetKeyPtr(ElementList, keyTaroColumnNumber, typeShortInteger, typeCode,
  1572.                                 (Ptr)&aHCellSize, sizeof(aHCellSize), actualSize));
  1573.         fServerHCellSize = RetranslateCellSize(aHCellSize);
  1574.         FailOSErr(AEGetKeyPtr(ElementList, keyTaroRowNumber, typeShortInteger, typeCode,
  1575.                                 (Ptr)&aVCellSize, sizeof(aVCellSize), actualSize));
  1576.         fServerVCellSize = RetranslateCellSize(aVCellSize);
  1577.         FailOSErr(AEGetKeyPtr(ElementList, keyTaroColor, typeEnumerated, typeCode,
  1578.                                 (Ptr)&aColor, sizeof(aColor), actualSize));
  1579.         fServerColor = (char)aColor;
  1580.         FailOSErr(AEGetKeyPtr(ElementList, keyTaroUserName, typeChar, typeCode,
  1581.                                 NULL, 0, actualSize));
  1582.         Handle    ClientName = NewHandle(actualSize);
  1583.         FailNIL(ClientName);
  1584.         LockHandleHigh(ClientName);
  1585.         FailOSErr(AEGetKeyPtr(ElementList, keyTaroUserName, typeChar, typeCode,
  1586.                                 (Ptr)(*ClientName), actualSize, actualSize));
  1587.         HUnlock(ClientName);
  1588.         GetIText(ClientName, aName);
  1589.         fNewServer = (TAppleEventPlayer *)fMessage->GetTransactionID();
  1590.         DisposHandle(ClientName);
  1591.         ((TServerTarget *)(fNewServer->ReturnTarget()))->SetName(aName);
  1592.         FailOSErr(AEGetKeyDesc(ElementList, keyTaroTableSpecifier, typeWildCard, targetSpec));
  1593.         fNewServer->SetTargetTableSpecifier(targetSpec);
  1594.         FailOSErr(AEDisposeDesc(ElementList));
  1595.         FailOSErr(AEDisposeDesc(targetSpec));
  1596.     }
  1597.     else
  1598.         ReportError(errAEFail, 0);
  1599. }
  1600.  
  1601. // ------------------------------------------------------------------------
  1602. #pragma segment ASelCommand
  1603. pascal void TNewElementCommand::DoIt(void)    
  1604. {
  1605.     FailInfo fi;
  1606.     if (fi.Try())
  1607.     {
  1608.         AEDesc    aTargetObject;
  1609.         
  1610.         gTaroApplication->OpenNew(cNewServer);
  1611.         TCoreDocument * aCoreDocument = fNewServer->GetCoreDocument();
  1612.         if(IsObject(aCoreDocument))
  1613.             aCoreDocument->CreateTableSpecifier(aTargetObject);
  1614.         else
  1615.         {
  1616.             FailOSErr(AECreateDesc(typeNull, NULL, 0, aTargetObject));
  1617.             ReportError(errAEUserCanceled, 0);
  1618.         }
  1619.         HandleReply(aTargetObject);
  1620.         fi.Success();
  1621.     }
  1622.     else
  1623.     {
  1624.         AEDesc    dNull;
  1625.         FailOSErr(AECreateDesc(typeNull, NULL, 0, dNull));
  1626.         ReportError(errAEFail, 0);
  1627.         HandleReply(dNull);
  1628.         fi.ReSignal();
  1629.     }
  1630. }
  1631.  
  1632. // ------------------------------------------------------------------------
  1633. #pragma segment ASelCommand
  1634. pascal void TNewElementCommand::HandleReply(AEDesc & aTargetObject)    
  1635. {
  1636.     gTaroApplication->RemoveNewElementCommand();
  1637.     fReply->WriteParameter(keyDirectObject, aTargetObject);
  1638.     fReply->SetTransactionID(fNewServer->GetTargetTransactionID());
  1639.     fCommandDone = TRUE;
  1640.     FailOSErr(AEDisposeDesc(aTargetObject));
  1641. }
  1642.  
  1643. // ------------------------------------------------------------------------
  1644. #pragma segment AOpen
  1645. pascal void TNewElementCommand::GetServerParameter(char & aHCellSize, char & aVCellSize, char & aColor,
  1646.                                         TAppleEventPlayer * (& aServer))
  1647. {
  1648.     aHCellSize = fServerHCellSize;
  1649.     aVCellSize = fServerVCellSize;
  1650.     aColor = fServerColor;
  1651.     aServer = fNewServer;
  1652. }
  1653.  
  1654.  
  1655. // ============================== TBeginTransactionCommand =================================
  1656. #pragma segment AOpen
  1657. pascal void TBeginTransactionCommand::IBeginTransactionCommand(AppleEvent & theAppleEvent, AppleEvent & Reply)
  1658. {
  1659.     InitializeFromAppleEvent(0, NULL, FALSE, FALSE, NULL, theAppleEvent, Reply);
  1660. }
  1661.  
  1662. // ------------------------------------------------------------------------
  1663. #pragma segment ADoCommand
  1664. pascal void TBeginTransactionCommand::DoIt(void)
  1665. {
  1666.     FailInfo    fi;
  1667.  
  1668.     if(fi.Try())
  1669.     {
  1670.         AEAddressDesc    address;
  1671.         AppleEvent aReply = fReply->fMessage;
  1672.         FailOSErr(AEGetAttributeDesc(aReply, keyAddressAttr, typeWildCard, address));
  1673.         TServerTarget * aServerTarget = new TServerTarget;
  1674.         aServerTarget->IServerTarget(address);
  1675.         TAppleEventPlayer * aPlayer = gTaroApplication->CreateServerPlayer();
  1676.         aPlayer->Initialize();
  1677.         aPlayer->SetTarget(aServerTarget);
  1678.         aPlayer->SetTargetTransactionID(fMessage->GetTransactionID());
  1679.         fReply->WriteLong(keyTransactionIDAttr, (long)aPlayer);
  1680.         fi.Success();
  1681.     }
  1682.     else
  1683.     {
  1684.         fReply->WriteLong(keyTransactionIDAttr, 0);
  1685.         ReportError(errAEFail, 0);
  1686.         fi.ReSignal();
  1687.     }
  1688.     fCommandDone = TRUE;
  1689. }
  1690.  
  1691. // ============================== TSetDataCommand =================================
  1692. #pragma segment AOpen
  1693. pascal void TSetDataCommand::ISetDataCommand(AppleEvent & theAppleEvent, AppleEvent & Reply)
  1694. {
  1695.     InitializeFromAppleEvent(0, NULL, FALSE, FALSE, NULL, theAppleEvent, Reply);
  1696. }
  1697.  
  1698. // ------------------------------------------------------------------------
  1699. #pragma segment ASelCommand
  1700. pascal void TSetDataCommand::DoIt(void)    
  1701. {
  1702.     FailInfo fi;
  1703.     long    longError = errAEFail;
  1704.  
  1705.     if (fi.Try())
  1706.     {
  1707.         TAppleEventPlayer * aPlayer = (TAppleEventPlayer *)fMessage->GetTransactionID();
  1708.         FailNonObject(aPlayer);
  1709.         if(IsMemberClassID(aPlayer, GetClassIDFromName("TAppleEventPlayer")))
  1710.         {
  1711.             AEDesc    ObjSpec, cellToken;
  1712.             DescType    actualType;
  1713.             Size    actualSize;
  1714.             long    theData;
  1715.             TRecordCell * aMove = NULL;
  1716.                 
  1717.             fMessage->ReadParameterPtr(keyAETheData, typeEnumerated, actualType,
  1718.                                       (Ptr)&theData, sizeof(theData), actualSize);
  1719.             if(theData == -1)
  1720.             {
  1721.                 aPlayer->MyMove(aMove);
  1722.                 longError = noErr;
  1723.             }
  1724.             else
  1725.             {
  1726.                 fMessage->ReadParameter(keyDirectObject, typeObjectSpecifier, ObjSpec);
  1727.                 FailOSErr(AEResolve(ObjSpec, kAEIDoMinimum, cellToken));
  1728.                 FailOSErr(AEDisposeDesc(ObjSpec));
  1729.                 if(cellToken.descriptorType == typeTObjectPtr)
  1730.                 {
  1731.                     aMove = (TRecordCell *)(**((TObjectPtrHandle)cellToken.dataHandle));
  1732.                     FailOSErr(AEDisposeToken(cellToken));
  1733.                     FailNonObject(aMove);
  1734.                     if(IsMemberClassID(aMove, GetClassIDFromName("TRecordCell")))
  1735.                     {
  1736.                         aMove->SetState((CellState)theData);
  1737.                         aPlayer->MyMove(aMove);
  1738.                         longError = noErr;
  1739.                     }
  1740.                     else
  1741.                         longError = errAENoSuchObject;
  1742.                 }
  1743.                 else
  1744.                     longError = errAENoSuchObject;
  1745.             }
  1746.         }
  1747.         else
  1748.             longError = errAEEventNotHandled;
  1749.         fi.Success();
  1750.     }
  1751.     else
  1752.     {
  1753.         ReportError(errAEFail,0);
  1754.         fi.ReSignal();
  1755.     }
  1756.     if(longError != noErr)
  1757.     {
  1758.         ReportError(errAEFail,0);
  1759.     }
  1760.     fCommandDone = TRUE;
  1761. }
  1762.  
  1763. // ============================== TMoveCommand =================================
  1764. #pragma segment AOpen
  1765. pascal void TMoveCommand::IMoveCommand(TPlayer * aPlayer, TRecordCell * aMove)
  1766. {
  1767.     fPlayer = aPlayer;
  1768.     fMove = aMove;
  1769.     ICommand(0, aPlayer->GetCoreDocument(), FALSE, FALSE, NULL);
  1770. }
  1771.  
  1772. // ------------------------------------------------------------------------
  1773. #pragma segment ASelCommand
  1774. pascal void TMoveCommand::DoIt(void)    
  1775. {
  1776.     fPlayer->MyMove(fMove);
  1777.     fCommandDone = TRUE;
  1778. }
  1779.  
  1780. // ============================== TSendTurnCommand =================================
  1781. #pragma segment AOpen
  1782. pascal void TSendTurnCommand::ISendTurnCommand(TCoreDocument * aCoreDocument)
  1783. {
  1784.     ICommand(0, aCoreDocument, FALSE, FALSE, NULL);
  1785. }
  1786.  
  1787. // ------------------------------------------------------------------------
  1788. #pragma segment ASelCommand
  1789. pascal void TSendTurnCommand::DoIt(void)    
  1790. {
  1791.     ((TCoreDocument *)fContext)->SendTurn();
  1792.     fCommandDone = TRUE;
  1793. }
  1794.